<template>
  <div class="app-container">
    <el-container>
      <el-header>
        <!--药品模板基本输入信息-->
        <el-form :model="queryParams" ref="queryForm" :inline="true" :rules="rules" v-show="showSearch" label-width="80px">
          <el-row>
            <el-col>
              <el-form-item>
                <el-button type="primary" @click="handleQuery" v-hasPermi="['dms:non_drug_model:add']">新建模板</el-button>
                <el-button type="danger" @click="resetQuery">取消</el-button>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col>
              <el-form-item label="模板名称" prop="name">
                <el-input
                  v-model="queryParams.name"
                  placeholder="请输入模版名称"
                  clearable
                  size="small"
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
              <el-form-item label="模板简介" prop="aim">
                <el-input
                  v-model="queryParams.aim"
                  placeholder="请输入模版简介"
                  clearable
                  size="small"
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
              <el-form-item label="范围" prop="scope">
                <el-select v-model="queryParams.scope" placeholder="请选择范围" clearable size="small">
                  <el-option
                    v-for="dict in scopeOptions"
                    :key="dict.dictValue"
                    :label="dict.dictLabel"
                    :value="parseInt(dict.dictValue)"
                  />
                </el-select>
              </el-form-item>
              <el-form-item label="模板类型" prop="type">
                <el-select v-model="queryParams.type" placeholder="请选择类型" clearable size="small">
                  <el-option
                    v-for="dict in typeOptions"
                    :key="dict.dictValue"
                    :label="dict.dictLabel"
                    :value="parseInt(dict.dictValue)"
                  />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </el-header>
      <!--非药品信息-->
      <el-main>
        <el-row style="margin-top: 50px">
          <el-col :span="1.5">
            <el-button
              type="primary"
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
            >编辑项目</el-button>
          </el-col>
        </el-row>
        <el-table :data="non_drugList">
          <el-table-column label="id" align="center" prop="id" v-show="showId=false"/>
          <el-table-column label="项目编码" align="center" prop="code" />
          <el-table-column label="项目名称" align="center" prop="name" />
          <el-table-column label="项目规划" align="center" prop="format" />
          <el-table-column label="项目单价(元)" align="center" prop="price" />
          <el-table-column label="所属费用科目" align="center" prop="expClassId" />
        </el-table>
      </el-main>
    </el-container>
    <!-- 添加或修改非药品模版管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <template>
        <el-transfer
          :titles="['非药品项目', '模板内容']"
          filterable
          filter-placeholder="项目名搜索"
          v-model="value"
          :data="data">
        </el-transfer>
      </template>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getNon_drug_model, addNon_drug_model, updateNon_drug_model } from "@/api/dms/non_drug_model";
import { getNon_drug_ids, getNonDrugByRecodeType } from "@/api/dms/non_drug"

export default {
  name: "Non_drug_model",
  data() {
    return {
      /** ----穿梭框—------*/
      data: [],
      value: [],
      /**-----------------------*/
      // 遮罩层
      loading: false,
      // 显示搜索条件
      showSearch: true,
      // 非药品表格数据
      non_drugList: [],
      // 弹出层标题
      title: "添加非药品项目",
      // 是否显示弹出层
      open: false,
      // 范围字典
      scopeOptions: [],
      // 模版类型字典
      typeOptions: [],
      // 模板参数
      queryParams: {
        name: null,
        scope: null,
        type: null,
        recordType: null,
      },
      // 表单校验
      rules: {
        name: [
          { required: true, message: "模板名称不能为空！", trigger: "blur" }
        ],
        aim: [
          { required: true, message: "模板简介不能为空！", trigger: "blur" }
        ],
        scope: [
          { required: true, message: "范围不能为空！", trigger: "change" }
        ],
        type: [
          { required: true, message: "模板类型不能为空！", trigger: "change" }
        ],
      },
    };
  },
  created() {
    this.getList();
    this.getDicts("dms_model_scoe").then(response => {
      this.scopeOptions = response.data;
    });
    this.getDicts("dms_non_drug_type").then(response => {
      this.typeOptions = response.data;
    });
  },
  methods: {
    /** -----------穿梭框------------- */
    /** 编辑项目按钮操作 */
    handleAdd() {
      if(this.queryParams.type != null){
        this.open = true;
        this.data = [];
        this.value = [];
        getNonDrugByRecodeType(this.queryParams.type).then(response => {
          response.data.map(nonDrug => {
            this.data.push({
              label: nonDrug.name,
              key: nonDrug.id,
            })
          })
        });
        this.non_drugList.map((nonDrug,index) => {
          this.value[index] = nonDrug.id
        })
      }else{
        this.$notify({
          title: '警告',
          message: '请选择模板类型',
          type: 'warning'
        });
      }
    },
    /** 穿梭框确定按钮 */
    submitForm() {
      this.non_drugList = [];
      if(this.value.length>0){
        let str = "";
        for(let i=0;i<this.value.length;i++){
          str += this.value[i]+",";
        }
        str = str.substring(0, str.length - 1);
        getNon_drug_ids(str).then(response => {//根据非药品id查询药品列表
          this.non_drugList = response.data;
        })
      }
      this.open = false;
    },
    // 穿梭框取消按钮
    cancel() {
      this.open = false;
    },
    /** 查询非药品模版管理列表 */
    getList() {
      if(this.$route.query.id != null){
        this.loading = true;
        getNon_drug_model(this.$route.query.id).then(response => {
          this.queryParams = response.data;
          if(response.data.nonDrugIdList != ""){
            getNon_drug_ids(response.data.nonDrugIdList).then(response => {//根据非药品id查询药品列表
              this.non_drugList = response.data;
            });
          }
          this.loading = false;
        });
      }
    },
    // 表单重置
    reset() {
      this.queryParams = {
        id: null,
        status: 0,
        name: null,
        nonDrugIdList: null,
        scope: null,
        ownId: null,
        aim: null,
        createTime: null,
        code: null,
        type: null
      };
      this.resetForm("queryParams");
    },
    /** 新建模板按钮操作 */
    handleQuery() {
      this.$refs["queryForm"].validate(valid => {
        if (valid) {
          let str = "";
          for(let i = 0;i<this.non_drugList.length;i++){
            str += this.non_drugList[i].id+",";
          }
          this.queryParams.nonDrugIdList = str.substring(0, str.length - 1);
          if (this.queryParams.id != null) {
            updateNon_drug_model(this.queryParams).then(response => {
             if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.$store.dispatch("tagsView/delView", this.$route);
                this.$router.push("/model/non_drug_model");
              }
            });
          } else {
            addNon_drug_model(this.queryParams).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.$store.dispatch("tagsView/delView", this.$route);
                this.$router.push("/model/non_drug_model");
              }
            });
          }
        }
      });
    },
    /** 取消,回退按钮操作 */
    resetQuery() {
      this.$store.dispatch("tagsView/delView", this.$route);
      this.$router.push("/model/non_drug_model");
    },

  }
};
</script>
